# Replication archive for:
# Galos and Coppock. 2023. 
# Gender Composition Predicts Gender Bias: A Meta-reanalysis of Hiring Discrimination Audit Experiments. 
# Science Advances. Forthcoming.

library(tidyverse)
library(metafor)
library(broom)
library(texreg)
source("code/helpers.R")

occupation_cates <- read_rds("data/occupation_cates.rds")
occupation_cates_ethnicity <- read_rds("data/occupation_cates_ethnicity.rds")

occupation_cates <- ungroup(occupation_cates)
occupation_cates_ethnicity <- ungroup(occupation_cates_ethnicity)

studies <- read_rds(file = "data/studies.rds")


occupation_cates <-
  occupation_cates %>%
  left_join(studies, by = "study_id")

# overall ---------------------------------------------------

fit_1 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female,
    data = occupation_cates
  )

fit_2 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female + as.factor(country),
    data = occupation_cates
  )

fit_3 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female + as.factor(first_year),
    data = occupation_cates
  )

fit_4 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female + as.factor(first_year) + as.factor(country),
    data = occupation_cates
  )


fit_5 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female + as.factor(study_id),
    data = occupation_cates
  )

texreg(list(fit_1, fit_2, fit_3, fit_4, fit_5), omit.coef = "as.factor", 
       custom.gof.rows = list(`Fixed Effects` = c("none", "country", "year", "country and year", "study")),
       custom.coef.names = c("Intercept", "Gender gradient"),
       caption = "Meta-regression estimates of the gender gradient",
       label = "tab:main")


# Ethnicity -------------------------------------------------

fit_1 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female,
    data = filter(occupation_cates_ethnicity, ethnicity_fct == "Majority")
  )

fit_2 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female,
    data = filter(occupation_cates_ethnicity, ethnicity_fct == "Minority")
  )

fit_3 <-
  rma.uni(
    yi = estimate,
    sei = std.error,
    mods = ~ fraction_female*ethnicity_fct,
    data = occupation_cates_ethnicity
  )

texreg(
  list(fit_1, fit_2, fit_3),
  custom.gof.rows = list(
    `Sample` = c("Majority status group", "Minority status group", "Both groups")
  ),
  custom.coef.names = c(
    "Intercept",
    "Gender gradient",
    "Majority status",
    "Majority x gradient"
  ),
  caption = "Meta-regression estimates of the gender gradient by majority / minority status",
  label = "tab:ethnicitycates"
)


